package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.mapper.TbStudentUserMapper;
import com.bangbang.pojo.TbStudentUser;
import com.bangbang.util.IdWorker;
import com.bangbang.util.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.Account;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-13
 */
@RestController
@RequestMapping("tbStudentUser")
public class TbStudentUserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TbStudentUserMapper tbStudentUserMapper;

        // 用户注册
        @ApiOperation(value = "用户注册")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "tbStudentUser",value = "用户表的实体类",required = true,paramType = "body"),
        })
        @PostMapping
        public Result studentRegister (@RequestBody TbStudentUser tbStudentUser){
            if ("".equals(tbStudentUser.getUserPassword())){
                return Result.buildResult(ResultEnum.ERROR).setResult("不能为空");
            }
            String salt = ShiroUtils.generateSalt(6);
            String password = tbStudentUser.getUserPassword();
            tbStudentUser.setUserPassword(ShiroUtils.encryptPassword("MD5", password, salt, 3));
            IdWorker idWorker = new IdWorker();
            long UserAccount = idWorker.nextId();
            String account = String.valueOf(UserAccount);
            tbStudentUser.setUserAccount(account).setUserSalt(salt);
            tbStudentUserMapper.insert(tbStudentUser);
            return Result.buildResult(ResultEnum.SUCCESS);
        }
//        用户的登录
    @ApiOperation(value = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "用户账号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "用户密码",required = true,paramType = "query"),
    })
    @GetMapping
    public Result Login(@RequestParam(required = true)String account,@RequestParam(required = true) String pwd){
        Subject subject = SecurityUtils.getSubject();
        String user = "1";
        UsernamePasswordToken token = new UsernamePasswordToken(account,pwd,user);
//        account  获取到登录用户的对象
        QueryWrapper<TbStudentUser> tbStudentUserMapperQueryWrapper = new QueryWrapper<>();
        tbStudentUserMapperQueryWrapper.eq("user_account", account);
        TbStudentUser tbStudentUser = tbStudentUserMapper.selectOne(tbStudentUserMapperQueryWrapper);
        String NowUser= UUID.randomUUID().toString();
        NowUser.replace("-","");
        redisTemplate.boundValueOps(NowUser).set(tbStudentUser.getUserId());
        return Result.buildResult(ResultEnum.SUCCESS).setResult(NowUser);//把这个uuid给前台,拿去判断
    }

}

